package com.example.demo.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Utils {

    private Utils(){}

    /**
     * 解析前端的json条件，封装成map
     */
    public static Map<String , String> parseMap(String jsonObject){
        Map<String, String> searchMap = new HashMap<>();
        if (StringUtils.isEmpty(jsonObject)) {
            return searchMap;
        }
        try {
            JSONObject jo = JSONObject.parseObject(jsonObject);
            for (Map.Entry<String, Object> item : jo.entrySet()) {
                String value = item.getValue().toString();
                if (!StringUtils.isEmpty(value)) {
                    searchMap.put(item.getKey(), value);
                }
            }
        } catch (Exception e) {
        }
        return searchMap;
    }

    /**
     * 产生excel的名字
     */
    public static String generateExcelName(String prefix){
        return prefix + "_" + DateKit.toStr(new Date(),"yyyyMMddHHmmss") + ".xls";
    }


    public static String getIp(HttpServletRequest request){
        return request.getHeader("X-Forwarded-For")==null?request.getRemoteAddr():request.getHeader("X-Forwarded-For");
    }
}
